package de.thm.exa.helpers;

import de.thm.exa.holders.Keyword;
import de.thm.exa.holders.Page;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class FileReader
{
    public static ArrayList<Page> read(File file) throws IOException
    {
        System.out.println("Reading file...");
        
        // Scan the file
        FileInputStream fis = new FileInputStream(file);
        Scanner scanner = new Scanner(fis);
        ArrayList<Page> pages = new ArrayList<>();
        
        int pageCount = scanner.nextInt();
        
        for( int i = 0 ; i < pageCount ; i++ )
        {
            String www = scanner.next();
            String hash = scanner.next();
            String pageRank = scanner.next();
            int keywordAmount = scanner.nextInt();
            
            ArrayList<Keyword> keywords = new ArrayList<>();
            for( int j = 0 ; j < keywordAmount ; j++ )
            {
                keywords.add( new Keyword( scanner.next(), scanner.nextInt() ) );
            }
            
            pages.add( new Page( www, hash, Double.parseDouble(pageRank), keywords ) );
            
            if( Debugger.LOGGING ) { System.out.println( pages.get(i).toString() ); }
            
            int skipAmount = scanner.nextInt();
            for(int j = 0 ; j < skipAmount ; j++)
            {
                scanner.next();
                int skipKeys = scanner.nextInt();
                for( int k = 0 ; k < skipKeys ; k++ )
                {
                    scanner.next();
                    scanner.next();
                }
            }
        }
        
        scanner.close();
        
        System.out.println("Ended reading file.");
        
        return pages;
    }
}